﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HY.DataAchieve.Dapper
{
    public interface IDapperHelper
    {
        /// <summary>
        /// 根据SQL查询列表
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="sql">SQL</param>
        /// <param name="param">参数</param>
        /// <param name="commandType">语句或存储过程</param>
        /// <param name="buffered">是否缓冲</param>
        /// <param name="commandTimeout">超时时间</param>
        /// <returns>查询结果泛型序列</returns>
        IEnumerable<T> Query<T>(string sql, object param = null, CommandType commandType = CommandType.Text, bool buffered = true, int? commandTimeout = null);

        /// <summary>
        /// 根据SQL查询实体
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="sql">SQL</param>
        /// <param name="param">参数</param>
        /// <param name="commandType">语句或存储过程</param>
        /// <param name="commandTimeout">超时时间</param>
        /// <returns></returns>
        T QueryFirstOrDefault<T>(string sql, object param = null, CommandType commandType = CommandType.Text, int? commandTimeout = null);

        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="sql">SQL</param>
        /// <param name="param">参数</param>
        /// <param name="commandType">语句或存储过程</param>
        /// <param name="commandTimeout">超时时间</param>
        /// <returns>受影响行数</returns>
        int Execute(string sql, object param = null, CommandType commandType = CommandType.Text, int? commandTimeout = null);

        /// <summary>
        /// 查询取值
        /// </summary>
        /// <param name="sql">查询字符串</param>
        /// <param name="param">参数</param>
        /// <param name="commandType">语句或存储过程</param>
        /// <param name="commandTimeout">超时时间</param>
        /// <returns></returns>
        object ExecuteScalar(string sql, object param = null, CommandType commandType = CommandType.Text, int? commandTimeout = null);

        /// <summary>
        /// 查询取值
        /// </summary>
        /// <typeparam name="T">返回值类型</typeparam>
        /// <param name="sql">查询字符串</param>
        /// <param name="param">参数</param>
        /// <param name="commandType">语句或存储过程</param>
        /// <param name="commandTimeout">超时时间</param>
        /// <returns></returns>
        T ExecuteScalar<T>(string sql, object param = null, CommandType commandType = CommandType.Text, int? commandTimeout = null);
        /// <summary>
        /// dapper通用分页方法
        /// </summary>
        /// <typeparam name="T">泛型集合实体类</typeparam>
        /// <param name="tableName">表</param>
        /// <param name="fields">列</param>
        /// <param name="where">条件</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">当前页显示条数</param>
        /// <param name="orderBy">排序</param>
        /// <returns></returns>
        Tuple<int, IEnumerable<T>> GetPageList<T>(string tableName, string fields, string condition, int pageIndex, int pageSize, string orderBy, CommandType commandType = CommandType.Text, bool buffered = true, int? commandTimeout = null);
    }
}
